home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / forth / pfe-0.000 / pfe-0 / pfe-0.9.13 / src / term.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-17  |  5.1 KB  |  209 lines

  1. /*
  2.  * This file is part of the portable Forth environment written in ANSI C.
  3.  * Copyright (C) 1995  Dirk Uwe Zoller
  4.  *
  5.  * This library is free software; you can redistribute it and/or
  6.  * modify it under the terms of the GNU Library General Public
  7.  * License as published by the Free Software Foundation; either
  8.  * version 2 of the License, or (at your option) any later version.
  9.  *
  10.  * This library is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13.  * See the GNU Library General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU Library General Public
  16.  * License along with this library; if not, write to the Free
  17.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  * This file is version 0.9.13 of 17-July-95
  20.  * Check for the latest version of this package via anonymous ftp at
  21.  *    roxi.rz.fht-mannheim.de:/pub/languages/forth/pfe-VERSION.tar.gz
  22.  * or    sunsite.unc.edu:/pub/languages/forth/pfe-VERSION.tar.gz
  23.  * or    ftp.cygnus.com:/pub/forth/pfe-VERSION.tar.gz
  24.  *
  25.  * Please direct any comments via internet to
  26.  *    duz@roxi.rz.fht-mannheim.de.
  27.  * Thank You.
  28.  */
  29. /*
  30.  * term.c ---   terminal i/o, system independent parts
  31.  * (duz 24Feb94)
  32.  */
  33.  
  34. #include "forth.h"
  35. #include "term.h"
  36.  
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <ctype.h>
  40.  
  41. #include "missing.h"
  42.  
  43.  
  44. /* global variables exported by the terminal stuff: */
  45.  
  46. int rows, cols;            /* size of text screen */
  47. int xmax, ymax;            /* size of graphics window in pixels */
  48.  
  49. /* map function and cursor keys to wordstar key sequences or to EKEY codes: */
  50.  
  51. static char            /* replacement strings for the key codes, */
  52. *raw2ws [NO_OF_KEYS] =        /* wordstar-like version for block editor: */
  53. {
  54.   "\013L",            /* F1 -> ^KL i.e. interpret this line */
  55.   "\013D",            /* F2 -> ^KD i.e. stamp screen */
  56.   "\013U",            /* F3 -> ^KU i.e. use other block file */
  57.   NULL,
  58.   "\013X",            /* F5 -> ^KX i.e. push line */
  59.   "\013E",            /* F6 -> ^KE i.e. pop line */
  60.   "\013Y",            /* F7 -> ^KY i.e. push and delete line */
  61.   "\013W",            /* F8 -> ^KW i.e. pop and insert line */
  62.   "\013B",            /* F9 -> ^KB i.e. load this block */
  63.   "\025",            /* F10 -> ^U i.e. quit editor */
  64.  
  65.   "\023", "\004", "\005", "\030",
  66.   "\021S","\021D","\003", "\022",
  67.   "\010", "\007", "\026", "\026",
  68.   "\016", "\021Y","\031", "\021Z",
  69. },
  70. *raw2ekey [NO_OF_KEYS] =    /* version for EKEY */
  71. {
  72.   "\000", "\001", "\002", "\003", "\004",
  73.   "\005", "\006", "\007", "\010", "\011",
  74.  
  75.   "\012", "\013", "\014", "\015",
  76.   "\016", "\017", "\020", "\021",
  77.   "\022", "\023", "\024", "\025",
  78.   "\026", "\027", "\030", "\031",
  79. };
  80.  
  81. static Byte keybuf [8];
  82. static Byte *keyptr = NULL;
  83.  
  84. static int
  85. get_remap_key (Byte *replace[])
  86. {
  87.   int i, n, m;
  88.  
  89.   if (keyptr)
  90.     if (*keyptr == '\0')
  91.       keyptr = NULL;
  92.     else
  93.       return *keyptr++;
  94.   n = 0;
  95.   for (;;)
  96.     {
  97.       keybuf[n++] = c_getkey ();
  98.       m = -1;
  99.       for (i = m; ++i < NO_OF_KEYS;)
  100.     {
  101.       if (!rawkey_string[i] || !replace[i])
  102.         continue;
  103.       if (memcmp (keybuf, rawkey_string[i], n) == 0)
  104.         if (rawkey_string[i][n] == '\0')
  105.           {
  106.         keyptr = replace[i];
  107.         return *keyptr++ | 0x100;
  108.           }
  109.         else
  110.           m = i;
  111.     }
  112.       if (m < 0)
  113.     {
  114.       keybuf[n] = '\0';
  115.       keyptr = keybuf;
  116.       return *keyptr++;
  117.     }
  118.     }
  119. }
  120.  
  121. int
  122. getekey (void)
  123. {
  124.   int key = get_remap_key ((Byte **) raw2ekey);
  125.  
  126.   if (key == EKEY_kb)
  127.     return '\b';
  128.   return key;
  129. }
  130.  
  131. int
  132. getwskey (void)
  133. {
  134.   return get_remap_key ((Byte **) raw2ws) & ~0x100;
  135. }
  136.  
  137. int
  138. ekeypressed (void)
  139. {
  140.   return (keyptr && *keyptr) || c_keypressed ();
  141. }
  142.  
  143. int
  144. printable (int c)
  145. {
  146.   return
  147. #if defined IBMPC_CHARSET
  148.     !isascii (c) || !iscntrl (c)    /* almost anything can be displayed */
  149. #else
  150.     isprint (c)
  151. #if defined (ISO_CHARSET)
  152.     || (0xA0 <= (Byte) c && (Byte) c <= 0xFF)
  153. #endif
  154. #endif
  155.     ;
  156. }
  157.  
  158. void                /* write character to screen */
  159. c_putc_printable (int c)    /* ensure visible result */
  160. {
  161.   if (printable (c))
  162.     c_putc (c);
  163.   else
  164.     {
  165.       c_standout_on ();
  166.       c_putc ((Byte) c < ' ' ? c + '@' : '?');
  167.       c_standout_off ();
  168.     }
  169. }
  170.  
  171. int
  172. change_case (int ch)
  173. {
  174.   if (ch >= 0x100)  return ch;
  175.   if (islower (ch)) return toupper (ch);
  176.   if (isupper (ch)) return tolower (ch);
  177. #if defined ISO_CHARSET
  178.   if (0xC0 <= (Byte)ch && (Byte)ch <= 0xDE) return ch + 0x20;
  179.   if (0xE0 <= (Byte)ch && (Byte)ch <= 0xFE) return ch - 0x20;
  180. #elif defined IBMPC_CHARSET
  181.   {
  182.     static char lower [] =
  183.       "\x81\x82\x83\x84\x85\x86\x87\x88" /* IBM 437 lower case characters */
  184.       "\x89\x8A\x8B\x8C\x8D\x91\x93\x94"
  185.       "\x95\x96\x97\x98\xA0\xA1\xA2\xA3"
  186.       "\xA4";
  187.     static char upper [] =
  188.       "\x9A\x90\x41\x8E\x41\x8F\x80\x45" /* IBM 437 upper case characters */
  189.       "\x45\x45\x49\x49\x49\x92\x4F\x99"
  190.       "\x4F\x55\x55\x59\x41\x49\x4F\x55"
  191.       "\xA5";
  192.     char *p;
  193.  
  194.     if ((p = strchr (lower, (char)ch))) return upper [p - lower];
  195.     if ((p = strchr (upper, (char)ch))) return lower [p - upper];
  196.   }
  197. #endif
  198.   return ch;
  199. }
  200.  
  201.  
  202. /*
  203.  * hooks for window size change and job control:
  204.  */
  205.  
  206. void (*on_stop) (void) =    system_terminal;
  207. void (*on_continue) (void) =    interactive_terminal;
  208. void (*on_winchg) (void) =    query_winsize;
  209.